home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utmisc1 / alcun101.lha / src / dump2rom.c < prev    next >
C/C++ Source or Header  |  1995-01-11  |  5KB  |  228 lines

  1. /*
  2.  *  This file is part of x48, an emulator of the HP-48sx Calculator.
  3.  *  Copyright (C) 1994  Eddie C. Dost  (ecd@dressler.de)
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* $Log: dump2rom.c,v $
  21.  * Revision 1.7  1995/01/11  18:20:01  ecd
  22.  * major update to support HP48 G/GX
  23.  *
  24.  * Revision 1.6  1994/12/07  20:20:50  ecd
  25.  * minor changes
  26.  *
  27.  * Revision 1.6  1994/12/07  20:20:50  ecd
  28.  * minor changes
  29.  *
  30.  * Revision 1.5  1994/11/28  02:00:51  ecd
  31.  * improved trapping of EOF
  32.  *
  33.  * Revision 1.4  1994/11/02  14:40:38  ecd
  34.  * support for "compressed" rom files added
  35.  *
  36.  * Revision 1.3  1994/09/18  15:29:22  ecd
  37.  * turned off unused rcsid message
  38.  *
  39.  * Revision 1.2  1994/09/13  16:57:00  ecd
  40.  * changed to plain X11
  41.  *
  42.  * Revision 1.1  1994/09/07  12:57:36  ecd
  43.  * Initial revision
  44.  *
  45.  * $Id: dump2rom.c,v 1.7 1995/01/11 18:20:01 ecd Exp ecd $
  46.  */
  47.  
  48.  
  49. #include "global.h"
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <unistd.h>
  54. #include <string.h>
  55. #ifdef SUNOS
  56. #include <memory.h>
  57. #endif
  58. #include <fcntl.h>
  59.  
  60. unsigned char *core;
  61.  
  62. #define DEFAULT_ROM_FILE "rom.dump"
  63.  
  64. int
  65. #ifdef __FunctionProto__
  66. write_mem_file(char *name, unsigned char *mem, int size)
  67. #else
  68. write_mem_file(name, mem, size)
  69. char *name;
  70. unsigned char *mem;
  71. int size;
  72. #endif
  73. {
  74.   FILE *fp;
  75.   unsigned char *tmp_mem;
  76.   unsigned char byte;
  77.   int i, j;
  78.  
  79.   if (NULL == (fp = fopen(name, "w")))
  80.     {
  81.       fprintf(stderr, "can\'t open %s\n", name);
  82.       return 0;
  83.     }
  84.  
  85.   if (NULL == (tmp_mem = (unsigned char *)malloc((size_t)size / 2)))
  86.     {
  87.       for (i = 0, j = 0; i < size / 2; i++)
  88.         {
  89.           byte = (mem[j++] & 0x0f);
  90.           byte |= (mem[j++] << 4) & 0xf0;
  91.           if (1 != fwrite(&byte, 1, 1, fp))
  92.             {
  93.               fprintf(stderr, "can\'t write %s\n", name);
  94.               fclose(fp);
  95.               return 0;
  96.             }
  97.         }
  98.     }
  99.   else
  100.     {
  101.       for (i = 0, j = 0; i < size / 2; i++)
  102.         {
  103.           tmp_mem[i] = (mem[j++] & 0x0f);
  104.           tmp_mem[i] |= (mem[j++] << 4) & 0xf0;
  105.         }
  106.  
  107.       if (fwrite(tmp_mem, 1, (size_t)size / 2, fp) != size / 2)
  108.         {
  109.           fprintf(stderr, "can\'t write %s\n", name);
  110.           fclose(fp);
  111.           free(tmp_mem);
  112.           return 0;
  113.        }
  114.  
  115.        free(tmp_mem);
  116.     }
  117.  
  118.   fclose(fp);
  119.   return 1;
  120. }
  121.  
  122. int
  123. #ifdef __FunctionProto__
  124. main(int argc, char **argv)
  125. #else
  126. main(argc, argv)
  127. int argc;
  128. char **argv;
  129. #endif
  130. {
  131.   FILE *dump;
  132.   long addr, size;
  133.   char ch;
  134.   int i, gx, error;
  135.  
  136.   if (argc < 2) {
  137.     fprintf(stderr, "usage: %s hp48-dump-file\n", argv[0]);
  138.     exit (1);
  139.   }
  140.  
  141.   if ((dump = fopen(argv[1], "r")) == NULL) {
  142.     fprintf(stderr, "%s: can\'t open %s\n", argv[0], argv[1]);
  143.     exit (1);
  144.   }
  145.  
  146.   if ((core = (unsigned char *)malloc(0x100000)) == NULL) {
  147.     fprintf(stderr, "%s: can\'t malloc %d bytes\n", argv[0], 0x100000);
  148.     exit (1);
  149.   }
  150.   memset(core, 0, 0x100000);
  151.  
  152.   gx = 0;
  153.   error = 0;
  154.   while (1) {
  155.     addr = 0;
  156.     for (i = 0; i < 5; i++) {
  157.       addr <<= 4;
  158.       if ((ch = fgetc(dump)) < 0) {
  159.         error = 1;
  160.         break;
  161.       }
  162.       if (ch >= '0' && ch <= '9') {
  163.         addr |= ch - '0';
  164.       } else if (ch >= 'A' && ch <= 'F') {
  165.         addr |= ch - 'A' + 10;
  166.       } else {
  167.         fprintf(stderr, "%s: Illegal char %c at %lx\n", argv[0], ch, addr);
  168.         error = 1;
  169.         break;
  170.       }
  171.     }
  172.     if (error)
  173.       break;
  174.     if (addr >= 0x80000)
  175.       gx = 1;
  176.     if ((ch = fgetc(dump)) < 0) {
  177.       fprintf(stderr, "%s: Unexpected EOF at %lx\n", argv[0], addr);
  178.       break;
  179.     }
  180.     if (ch != ':') {
  181.       fprintf(stderr, "%s: Illegal char %c, expected \':\' at %lx\n",
  182.                       argv[0], ch, addr);
  183.       break;
  184.     }
  185.     for (i = 0; i < 16; i++) {
  186.       if ((ch = fgetc(dump)) < 0) {
  187.         fprintf(stderr, "%s: Unexpected EOF at %lx\n", argv[0], addr);
  188.         error = 1;
  189.         break;
  190.       }
  191.       if (ch >= '0' && ch <= '9') {
  192.         core[addr++] = ch - '0';
  193.       } else if (ch >= 'A' && ch <= 'F') {
  194.         core[addr++] = ch - 'A' + 10;
  195.       } else {
  196.         fprintf(stderr, "%s: Illegal char %c at %lx\n", argv[0], ch, addr);
  197.         error = 1;
  198.         break;
  199.       }
  200.     }
  201.     if (error)
  202.       break;
  203.     if ((ch = fgetc(dump)) < 0)
  204.       break;
  205.     if (ch != '\n') {
  206.       fprintf(stderr, "%s: Illegal char %c, expected \'\\n\' at %lx\n",
  207.                       argv[0], ch, addr);
  208.       break;
  209.     }
  210.   }
  211.  
  212.   if (!gx)
  213.     if (core[0x29] == 0x0)
  214.       gx = 1;
  215.   if (gx)
  216.     size = 0x100000;
  217.   else
  218.     size = 0x80000;
  219.   if (!write_mem_file(DEFAULT_ROM_FILE, core, size))
  220.     {
  221.       fprintf(stderr, "%s: can\'t write to %s\n", argv[0], DEFAULT_ROM_FILE);
  222.       exit (1);
  223.     }
  224.  
  225.   exit (0);
  226. }
  227.  
  228.